package vip.george.dynamic.jdkproxy;

import vip.george.dynamic.jdkproxy.handler.LoggingHandler;
import vip.george.dynamic.jdkproxy.service.UserService;
import vip.george.dynamic.jdkproxy.service.impl.UserServiceImpl;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;


/**
 * 动态代理测试
 */
public class DynamicProxyDemo {
    public static void main(String[] args) {
        // 创建实际对象
        UserService realService = new UserServiceImpl();

        // 创建调用处理器
        InvocationHandler handler = new LoggingHandler(realService);

        // 创建代理对象
        UserService proxyService = (UserService) Proxy.newProxyInstance(
                UserService.class.getClassLoader(), // 类加载器
                new Class<?>[] { UserService.class }, // 代理接口数组
                handler // 调用处理器
        );

        // 使用代理对象
        proxyService.addUser("张三");
        String user = proxyService.getUser("李四");
        proxyService.deleteUser("王五");

        System.out.println("代理类: " + proxyService.getClass().getName());
    }
}
